package cn.stylefeng.guns.modular.sakana.devopsdohostmachine.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;
import cn.stylefeng.guns.modular.sakana.devopsdohostmachine.entity.DevopsDoHostMachine;
import cn.stylefeng.guns.modular.sakana.devopsdohostmachine.enums.DevopsDoHostMachineExceptionEnum;
import cn.stylefeng.guns.modular.sakana.devopsdohostmachine.mapper.DevopsDoHostMachineMapper;
import cn.stylefeng.guns.modular.sakana.devopsdohostmachine.pojo.request.DevopsDoHostMachineRequest;
import cn.stylefeng.guns.modular.sakana.devopsdohostmachine.service.DevopsDoHostMachineService;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Date;

/**
 * 主机表业务实现层
 *
 * @author sakanayy
 * @date 2023/06/29 15:15
 */
@Service
public class DevopsDoHostMachineServiceImpl extends ServiceImpl<DevopsDoHostMachineMapper, DevopsDoHostMachine> implements DevopsDoHostMachineService {

	@Override
    public void add(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        DevopsDoHostMachine devopsDoHostMachine = new DevopsDoHostMachine();
        BeanUtil.copyProperties(devopsDoHostMachineRequest, devopsDoHostMachine);
        this.save(devopsDoHostMachine);
    }

    @Override
    public void del(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        DevopsDoHostMachine devopsDoHostMachine = this.queryDevopsDoHostMachine(devopsDoHostMachineRequest);
        this.removeById(devopsDoHostMachine.getHostMachineId());
    }

    @Override
    public void edit(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        DevopsDoHostMachine devopsDoHostMachine = this.queryDevopsDoHostMachine(devopsDoHostMachineRequest);
        BeanUtil.copyProperties(devopsDoHostMachineRequest, devopsDoHostMachine);
        this.updateById(devopsDoHostMachine);
    }

    @Override
    public DevopsDoHostMachine detail(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        return this.queryDevopsDoHostMachine(devopsDoHostMachineRequest);
    }

    @Override
    public PageResult<DevopsDoHostMachine> findPage(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        LambdaQueryWrapper<DevopsDoHostMachine> wrapper = createWrapper(devopsDoHostMachineRequest);
        Page<DevopsDoHostMachine> sysRolePage = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(sysRolePage);
    }

    @Override
    public List<DevopsDoHostMachine> findList(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        LambdaQueryWrapper<DevopsDoHostMachine> wrapper = this.createWrapper(devopsDoHostMachineRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author sakanayy
     * @date 2023/06/29 15:15
     */
    private DevopsDoHostMachine queryDevopsDoHostMachine(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        DevopsDoHostMachine devopsDoHostMachine = this.getById(devopsDoHostMachineRequest.getHostMachineId());
        if (ObjectUtil.isEmpty(devopsDoHostMachine)) {
            throw new ServiceException(DevopsDoHostMachineExceptionEnum.DEVOPS_DO_HOST_MACHINE_NOT_EXISTED);
        }
        return devopsDoHostMachine;
    }

    /**
     * 创建查询wrapper
     *
     * @author sakanayy
     * @date 2023/06/29 15:15
     */
    private LambdaQueryWrapper<DevopsDoHostMachine> createWrapper(DevopsDoHostMachineRequest devopsDoHostMachineRequest) {
        LambdaQueryWrapper<DevopsDoHostMachine> queryWrapper = new LambdaQueryWrapper<>();

        Long hostMachineId = devopsDoHostMachineRequest.getHostMachineId();
        String hostMachineName = devopsDoHostMachineRequest.getHostMachineName();
        String hostMachineIp = devopsDoHostMachineRequest.getHostMachineIp();
        String operatingSystem = devopsDoHostMachineRequest.getOperatingSystem();
        String sshPort = devopsDoHostMachineRequest.getSshPort();
        Integer authenticationType = devopsDoHostMachineRequest.getAuthenticationType();
        String hostMachineUserName = devopsDoHostMachineRequest.getHostMachineUserName();
        String hostMachinePassword = devopsDoHostMachineRequest.getHostMachinePassword();
        String hostSecretKeyCipher = devopsDoHostMachineRequest.getHostSecretKeyCipher();
        Integer hostMachineState = devopsDoHostMachineRequest.getHostMachineState();
        String companyFlag = devopsDoHostMachineRequest.getCompanyFlag();

        queryWrapper.eq(ObjectUtil.isNotNull(hostMachineId), DevopsDoHostMachine::getHostMachineId, hostMachineId);
        queryWrapper.like(ObjectUtil.isNotEmpty(hostMachineName), DevopsDoHostMachine::getHostMachineName, hostMachineName);
        queryWrapper.like(ObjectUtil.isNotEmpty(hostMachineIp), DevopsDoHostMachine::getHostMachineIp, hostMachineIp);
        queryWrapper.like(ObjectUtil.isNotEmpty(operatingSystem), DevopsDoHostMachine::getOperatingSystem, operatingSystem);
        queryWrapper.like(ObjectUtil.isNotEmpty(sshPort), DevopsDoHostMachine::getSshPort, sshPort);
        queryWrapper.eq(ObjectUtil.isNotNull(authenticationType), DevopsDoHostMachine::getAuthenticationType, authenticationType);
        queryWrapper.like(ObjectUtil.isNotEmpty(hostMachineUserName), DevopsDoHostMachine::getHostMachineUserName, hostMachineUserName);
        queryWrapper.like(ObjectUtil.isNotEmpty(hostMachinePassword), DevopsDoHostMachine::getHostMachinePassword, hostMachinePassword);
        queryWrapper.like(ObjectUtil.isNotEmpty(hostSecretKeyCipher), DevopsDoHostMachine::getHostSecretKeyCipher, hostSecretKeyCipher);
        queryWrapper.eq(ObjectUtil.isNotNull(hostMachineState), DevopsDoHostMachine::getHostMachineState, hostMachineState);
        queryWrapper.like(ObjectUtil.isNotEmpty(companyFlag), DevopsDoHostMachine::getCompanyFlag, companyFlag);

        return queryWrapper;
    }

}